#include "Weapon.h"
#include "StaticData.h"
using namespace cocos2d;

Weapon::Weapon()
	:_cannon(0)
	,_bullet(0)
	,_fishingNet(0)
{
}

Weapon::~Weapon()
{
}

bool Weapon::init(CannonType type/*=k_Cannon_Type_1*/)
{
	_cannon=Cannon::create(type);
	addChild(_cannon);

	_bullet=Bullet::create();
	_bullet->setVisible(false);
	addChild(_bullet);

	_fishingNet=FishingNet::create();
	_fishingNet->setVisible(false);
	addChild(_fishingNet);

	//_particle = CCParticleSystemQuad::create(StaticData::getInstance()->stringFromKey("star_particle"));
	//_particle->stopSystem();
	//this->addChild(_particle);

	return true;
}

Weapon* Weapon::create(CannonType type/*=k_Cannon_Type_1*/)
{
	Weapon* pRet=new Weapon;
	pRet->init(type);
	pRet->autorelease();
	return pRet;
}

CannonType Weapon::getCannonType()
{
	return _cannon->getType();
}

bool Weapon::shootTo(CCPoint touchLocation)
{
	if(getStatus()!=k_Weapon_Status_None)
		return false;
	float distance=180*(_cannon->getType()+1);
	CCPoint normal=ccpNormalize(ccpSub(touchLocation,getParent()->convertToWorldSpace(getPosition())));
	CCPoint vector=ccpMult(normal,distance);
	CCPoint target=ccpAdd(getPosition(),vector);
	_bullet->flyTo(target);
	return true;
}

WeaponStatus Weapon::getStatus()
{
	if(_bullet->isVisible()){
		return k_Weapon_Status_Bullet;
	}else if(_fishingNet->isVisible()){
		return k_Weapon_Status_FishingNet;
	}else{
		return k_Weapon_Status_None;
	}
}

void Weapon::aimAt(cocos2d::CCPoint target)
{
	_cannon->aimAt(target);
}

void Weapon::end()
{
	CCPoint pos=_bullet->getPosition();
	_bullet->end();
	//_particle->setPosition(pos);
	//_particle->resetSystem();
	_fishingNet->showAt(pos);
}

CCPoint Weapon::getCollisionPoint()
{
	CCAssert(getStatus()==k_Weapon_Status_Bullet,"Do Not Call It Before Shooting");
	if(getStatus()==k_Weapon_Status_Bullet){
		return _bullet->getCollisionPoint();
	}
	return CCPointZero;
}

CCRect Weapon::getCollisionArea()
{
	CCAssert(getStatus()==k_Weapon_Status_FishingNet,"Weapon is not fishingnet!");
	if(getStatus()==k_Weapon_Status_FishingNet){
		return _fishingNet->getCollisionArea();
	}
	return CCRectZero;
}
